<!-- notice表单弹窗 -->
<div id="notice-form" lay-filter="notice-form" class="layui-form model-form">
    <input id="nticid" name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">通知标题</label>
        <div class="layui-input-block">
            <input name="ntctle" id="ntctle" placeholder="请填写标题" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">通知内容</label>
        <div class="layui-input-block">
            <textarea id="contnt" name="contnt"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">截止时间</label>
        <div class="layui-input-block">
            <input name="clsdat" id="clsdat" placeholder="请选择截止时间" type="text" class="layui-input"
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">通知角色</label>
        <div class="layui-input-block">
            <!--            <select name="roleId" xm-select="roleId" id="xm-select" lay-verify="required">-->
            <!--            </select>-->
            <div id="xm-select"></div>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="notice-form-submit" lay-submit>发布</button>
    </div>
</div>

<!--<script src="../../module/xm-select/xm-select.js"></script>-->
<script>
    layui.use(['layer', 'admin', 'form', 'index', 'xmSelect', 'tinymce'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var laydate = layui.laydate;
        var xmSelect = layui.xmSelect;
        var tinymce = layui.tinymce;
        var index = layui.index;
        var userinfo = '';

        index.getUser(function (user) {
            userinfo = user;
        })
        //渲染富文本框
        tinymce.render({
            elem: "#contnt",
            height: '200px'
            // 支持tinymce所有配置
        }, (opt, edit) => {
            // 加载完成后回调 opt 是传入的所有参数
            // edit是当前编辑器实例，等同于t.get返回值
        });
        let edit = tinymce.get("#contnt");
        //渲染日期选择器
        laydate.render({
            elem: "#clsdat",
            type: 'datetime'
        })
        //渲染form表单中的单选框
        form.render('radio');
        //加载层
        layer.load(2);
        // 获取所有角色
        var xmrole;
        admin.req('api-user/roles', {}, function (data) {
            console.log(data);
            if (0 == data.code) {
                // 渲染多选下拉框
                var roleSelectData = new Array();
                for (var i = 0; i < data.data.length; i++) {
                    roleSelectData.push({name: data.data[i].name, value: data.data[i].id});
                }
                //全选, 清空, 反选
                xmrole = xmSelect.render({
                    el: '#xm-select',
                    language: 'zn',
                    toolbar: {
                        show: true,
                        list: ['ALL', 'CLEAR', 'REVERSE']
                    },
                    data: roleSelectData
                });

                let notice = admin.getTempData("t_notice");
                if (notice) {
                    $("#ntctle").val(notice.ntctle);
                    console.log(notice.contnt);
                    $("#contnt").val(notice.contnt);
                    edit.setContent(notice.contnt);
                    // edit.insertContent(notice.contnt);
                    $("#clsdat").val(notice.clsdat);
                    var rds = new Array();
                    for (var i = 0; i < notice.roles.length; i++) {
                        rds.push(notice.roles[i].id);
                    }
                    xmrole.setValue(rds);
                }
            } else {
                layer.msg('获取角色失败', {icon: 2, time: 500});
            }
            //关闭加载层
            layer.closeAll('loading');
        }, 'GET');

        form.on('submit(notice-form-submit)', function () {
            layer.load(2);
            let notice = admin.getTempData("t_notice");
            let nticid;
            if (notice == undefined)
                nticid = null;
            else
                nticid = notice.nticid;
            let ntctle = $("#ntctle").val();
            let contnt = edit.getContent();
            let clsdat = $("#clsdat").val();
            let roleId = xmrole.getValue('valueStr');

            notice = {
                nticid: nticid,
                ntctle: ntctle,
                contnt: contnt,
                clsdat: clsdat,
                pbsher: userinfo.username,
                roleId: roleId
            };
            console.log(notice);
            admin.req('api-user/notice/publishOrUpdateNotice', JSON.stringify(notice), function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
            }, 'POST');
            return false;
        });
    });
</script>